package com.xiaomi.shoppinglist

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update

@Dao
interface ShoppingItemDao {
    // 插入购物项
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(item: ShoppingItem): Long

    // 更新购物项
    @Update
    suspend fun update(item: ShoppingItem)

    // 删除购物项
    @Delete
    suspend fun delete(item: ShoppingItem)

    // 获取所有未完成的一次性购物项
    @Query("SELECT * FROM shopping_items WHERE isRepeatable = 0 AND isCompleted = 0 ORDER BY createTime DESC")
    suspend fun getActiveOneTimeItems(): List<ShoppingItem>

    // 获取所有未完成的可重复购物项
    @Query("SELECT * FROM shopping_items WHERE isRepeatable = 1 AND isCompleted = 0 ORDER BY createTime DESC")
    suspend fun getActiveRepeatableItems(): List<ShoppingItem>

    // 获取所有已完成的购物项（历史记录）
    @Query("SELECT * FROM shopping_items WHERE isCompleted = 1 ORDER BY completedTime DESC")
    suspend fun getCompletedItems(): List<ShoppingItem>

    // 根据ID获取购物项
    @Query("SELECT * FROM shopping_items WHERE id = :id")
    suspend fun getItemById(id: Long): ShoppingItem?

    // 获取需要提醒的可重复购物项
    @Query("SELECT * FROM shopping_items WHERE isRepeatable = 1 AND isCompleted = 1")
    suspend fun getRemindableItems(): List<ShoppingItem>
}